﻿using System;
using System.Collections.Generic;
using System.Linq;
using MEF_Test.Module.AudioStation.Tests.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;
using Synology.AudioStationApi;

namespace MEF_Test.Module.AudioStation.Tests
{


    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        private Semaphore _semaphore;

        [TestInitialize]
        public void InitializeSemaphore()
        {
            _semaphore = new Semaphore(0, 1);
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion


        [TestMethod]
        public void ParseAllMusicQuery()
        {
            // 1. Get a query result string
            // 2. Parse the JSON to generate an object.
            // 3. Make sure we have a valid mp3 path for one choosen item in the results

            // set a static query response (JSON): 
            const string query = "{\r\n   \"items\" : [\r\n      {\r\n         \"album\" : \"2009-12-29 - American Airlines Arena, Miami, Florida, USA (2009) CD2\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1655588.jpg\",\r\n         \"artist\" : \"Phish\",\r\n         \"bitrate\" : 260691,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 520,\r\n         \"genre\" : \"Jam Alternative Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1655588\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Phish/2009-12-29 - American Airlines Arena, Miami, Florida, USA (2009) CD2/03 Prince Caspian.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 0,\r\n         \"size\" : \"17242989\",\r\n         \"support\" : true,\r\n         \"title\" : \"Prince Caspian\",\r\n         \"track\" : 3,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648203.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 224899,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 249,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648203\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/01 Mie.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 1,\r\n         \"size\" : \"7107925\",\r\n         \"support\" : true,\r\n         \"title\" : \"Mie\",\r\n         \"track\" : 1,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648198.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 281505,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 299,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648198\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/02 La Cerva.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 2,\r\n         \"size\" : \"10617772\",\r\n         \"support\" : true,\r\n         \"title\" : \"La Cerva\",\r\n         \"track\" : 2,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648205.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 268974,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 451,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648205\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/03 Ghosts of the Garden City.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 3,\r\n         \"size\" : \"15258068\",\r\n         \"support\" : true,\r\n         \"title\" : \"Ghosts of the Garden City\",\r\n         \"track\" : 3,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648201.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 272109,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 303,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648201\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/04 Malacoda.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 4,\r\n         \"size\" : \"10415581\",\r\n         \"support\" : true,\r\n         \"title\" : \"Malacoda\",\r\n         \"track\" : 4,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648199.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 244839,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 200,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648199\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/05 Epochs in Dmaj.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 5,\r\n         \"size\" : \"6209004\",\r\n         \"support\" : true,\r\n         \"title\" : \"Epochs in Dmaj\",\r\n         \"track\" : 5,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648204.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 289041,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 376,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648204\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/06 Of Foam and Wave.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 6,\r\n         \"size\" : \"13685834\",\r\n         \"support\" : true,\r\n         \"title\" : \"Of Foam and Wave\",\r\n         \"track\" : 6,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648197.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 231113,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 266,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648197\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/07 Concrescence.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 7,\r\n         \"size\" : \"7774361\",\r\n         \"support\" : true,\r\n         \"title\" : \"Concrescence\",\r\n         \"track\" : 7,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648200.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 271770,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 430,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648200\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/08 The Raven.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 8,\r\n         \"size\" : \"14701342\",\r\n         \"support\" : true,\r\n         \"title\" : \"The Raven\",\r\n         \"track\" : 8,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648202.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 220341,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 374,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648202\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/09 Vienna.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 9,\r\n         \"size\" : \"10405483\",\r\n         \"support\" : true,\r\n         \"title\" : \"Vienna\",\r\n         \"track\" : 9,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"Tertia\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648196.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 258547,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 545,\r\n         \"genre\" : \"Post-Rock\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648196\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/Tertia/10 Sycamore.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 10,\r\n         \"size\" : \"17728551\",\r\n         \"support\" : true,\r\n         \"title\" : \"Sycamore\",\r\n         \"track\" : 10,\r\n         \"year\" : 2009\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648192.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 256362,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 547,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648192\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/01 Moksha.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 11,\r\n         \"size\" : \"17594279\",\r\n         \"support\" : true,\r\n         \"title\" : \"Moksha\",\r\n         \"track\" : 1,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648195.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 268200,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 326,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648195\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/02 Some Are White Light.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 12,\r\n         \"size\" : \"10976016\",\r\n         \"support\" : true,\r\n         \"title\" : \"Some Are White Light\",\r\n         \"track\" : 2,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648188.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 244722,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 323,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648188\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/03 Sea Lawn.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 13,\r\n         \"size\" : \"9930501\",\r\n         \"support\" : true,\r\n         \"title\" : \"Sea Lawn\",\r\n         \"track\" : 3,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648194.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 263470,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 457,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648194\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/04 Crawlspace.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 14,\r\n         \"size\" : \"15115634\",\r\n         \"support\" : true,\r\n         \"title\" : \"Crawlspace\",\r\n         \"track\" : 4,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648186.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 254024,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 343,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648186\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/05 Book IX.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 15,\r\n         \"size\" : \"10946912\",\r\n         \"support\" : true,\r\n         \"title\" : \"Book IX\",\r\n         \"track\" : 5,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648189.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 221630,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 125,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648189\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/06 The Dropsonde.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 16,\r\n         \"size\" : \"3503422\",\r\n         \"support\" : true,\r\n         \"title\" : \"The Dropsonde\",\r\n         \"track\" : 6,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648187.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 260188,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 358,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648187\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/07 Brombie.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 17,\r\n         \"size\" : \"11688268\",\r\n         \"support\" : true,\r\n         \"title\" : \"Brombie\",\r\n         \"track\" : 7,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648190.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 205505,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 203,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648190\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/08 Our Breaths In Winter.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 18,\r\n         \"size\" : \"5256206\",\r\n         \"support\" : true,\r\n         \"title\" : \"Our Breaths In Winter\",\r\n         \"track\" : 8,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648193.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 212340,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 184,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648193\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/09 The Dove.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 19,\r\n         \"size\" : \"4943130\",\r\n         \"support\" : true,\r\n         \"title\" : \"The Dove\",\r\n         \"track\" : 9,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648185.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 253099,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 436,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648185\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/10 ASA.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 20,\r\n         \"size\" : \"13852659\",\r\n         \"support\" : true,\r\n         \"title\" : \"ASA\",\r\n         \"track\" : 10,\r\n         \"year\" : 2007\r\n      },\r\n      {\r\n         \"album\" : \"The Four Trees\",\r\n         \"albumArtURL\" : \"webUI/getcover.cgi/ebdart/1648191.jpg\",\r\n         \"artist\" : \"Caspian\",\r\n         \"bitrate\" : 242297,\r\n         \"channels\" : 2,\r\n         \"class\" : \"object.item.audioItem.musicTrack\",\r\n         \"disc\" : 0,\r\n         \"duration\" : 314,\r\n         \"genre\" : \"\",\r\n         \"icon\" : \"icon_musicitem.png\",\r\n         \"is_container\" : false,\r\n         \"is_track\" : true,\r\n         \"item_id\" : \"musiclib_musicsearch/@1648191\",\r\n         \"item_pid\" : \"musiclib_root\",\r\n         \"protocolinfo\" : \"http-get:*:audio/mpeg:*\",\r\n         \"res\" : \"/volume1/music/iTunes Library/Caspian/The Four Trees/11 ...Reprise.mp3\",\r\n         \"sample\" : 44100,\r\n         \"sequence\" : 21,\r\n         \"size\" : \"9550036\",\r\n         \"support\" : true,\r\n         \"title\" : \"...Reprise\",\r\n         \"track\" : 11,\r\n         \"year\" : 2007\r\n      }\r\n   ],\r\n   \"success\" : true,\r\n   \"total\" : 22\r\n}\r\n";

            // Use a private Accessor to access the parseSynologyFiles private property.
            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost);

            IEnumerable<SynoTrack> result;
            try
            {
                long resultCount;

                // thanks to the private accessors, this private method is available in our unit tests !
                syno.parseSynologyFiles(query, out result, out resultCount);
            }
            catch (ArgumentNullException)
            {
                Assert.Fail("The JSON parser tried to read an inexisting property");
                throw;
            }

            var item = result.First(o => o.Res.Contains(".mp3"));
            Assert.IsNotNull(item.Res);
        }

        [TestMethod]
        public void SynoLogin()
        {
            // 1. The login/password is passed as arguments to the Login method of SynologyApi                       
            // 2. Check the returned string (Token) is not null

            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);

            // release the semaphore when the process is over 
            string token = syno.Login(Settings.Default.Login, Settings.Default.Password);

            Assert.IsNotNull(token);
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void SynoLoginSynchronouslyTest()
        {
            // 1. The login/password is passed as arguments to the Login method of SynologyApi
            // 2. the token is returned.
            var syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);
            var s = syno.Login(Settings.Default.Login, Settings.Default.Password);
            Assert.IsTrue(s.StartsWith("id=",StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void SynoLoginAsync()
        {
            Assert.Inconclusive("Depending on how many tests are run before this one : some multithreading errors occur");
            // 1. The login/password is passed as arguments to the Login method of SynologyApi
            // 2. The "LoginCompleted" event is registered
            // 3. The SynologyApi instances fires an event, carrying a token in the eventargs.
            // 4. The token is stored in a private field of the SynologyApi instance.

            var syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);

            Assert.IsFalse(syno._queryLock);

            string token = string.Empty;
            syno.add_LoginCompleted((s, e) =>
                                       {
                                           _semaphore.Release();
                                           token = e.TokenCookie;
                                       });
            syno.LoginAsync(Settings.Default.Login, Settings.Default.Password, null);

            // wait here untill the process is over : this is only so the test method doesn't end before the token has been received
            // Warning : This only works in the unit tests, not in a WPF Dispatcher !
            _semaphore.WaitOne();
            Assert.IsNotNull(token);
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void QueryByArtistWithoutLoggin()
        {
            // 1. Query an artist without logging before : an exception *MUST* be thrown
            // 2. The exception is of the right type
            var syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);
            try
            {
                syno.SearchAsync("Caspian", QueryType.Artist);
                Assert.Fail("An InvalidOperationException should have been raised");
            }
            catch (InvalidOperationException e)
            {
                // Of course it will pass.
                Assert.IsInstanceOfType(e, typeof(InvalidOperationException));
            }
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void QueryAudioRoot()
        {
            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);

            syno.Login(Settings.Default.Login, Settings.Default.Password);

            IEnumerable<SynoItem> jsonObject = SynoHelper.Search<SynoItem>(syno, string.Empty, QueryType.AudioRoot);

            Assert.IsTrue(jsonObject.Count() > 0);
            Assert.IsFalse(syno._queryLock);
        }


        [TestMethod]
        public void QueryByAlbum()
        {

            // 1. Log in Syno service
            // 2. Query an Album
            // 3. Parse the returned JSON file and return its object representation
            // 4. there are some elements returned by the query.
            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);

            syno.Login(Settings.Default.Login, Settings.Default.Password);

            IEnumerable<SynoItem> jsonObject = SynoHelper.Search<SynoItem>(syno, "Trees", QueryType.Album);

            Assert.IsTrue(jsonObject.First().Title.Contains("Trees"));
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void QueryByArtist()
        {

            // 1. Log in Syno service
            // 2. Query an Artist
            // 3. Parse the returned JSON file and return its object representation
            // 4. Check the first element has an existing path to an audio file.
            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);

            syno.Login(Settings.Default.Login, Settings.Default.Password);

            IEnumerable<SynoItem> jsonObject = SynoHelper.Search<SynoItem>(syno, "Caspian", QueryType.Artist);

            Assert.IsTrue(jsonObject.First().Title.Equals("Caspian", StringComparison.OrdinalIgnoreCase));
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void QueryByAlbumID()
        {
            // 1. Log in Syno service
            // 2. Query with the AlbumID signature
            // 3. Make sure the returned object contains some element

            // Caspian's The Four Trees
            const string albumId = "musiclib_music_aa/1648185/1648196";

            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);
            syno.Login(Settings.Default.Login, Settings.Default.Password);

            const QueryType queryType = QueryType.AlbumID;
            var result = SynoHelper.Search<SynoTrack>(syno, albumId, queryType);

            Assert.IsTrue(result.Count() > 0);
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void QueryByAlbumIdv2()
        {
            // 1. Log in Syno service
            // 2. Query with the AlbumID signature
            // 3. Make sure the returned object contains some element

            // Caspian's The Four Trees
            const string albumId = "musiclib_music_album/1648185";

            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);
            syno.Login(Settings.Default.Login, Settings.Default.Password);

            const QueryType queryType = QueryType.AlbumID;
            var result = SynoHelper.Search<SynoTrack>(syno, albumId, queryType);

            Assert.IsTrue(result.Count() > 0);
            Assert.IsFalse(syno._queryLock);
        }

        [TestMethod]
        public void QueryByArtistID()
        {
            // 1. Log in Syno service
            // 2. Query with the ArtistID signature
            // 3. Make sure the returned object contains some elements            

            // Caspian ID
            const string artistId = "musiclib_music_aa/1648185";

            AudioStationSession_Accessor syno = new AudioStationSession_Accessor(Settings.Default.SynoHost, Settings.Default.SynoPort);
            syno.Login(Settings.Default.Login, Settings.Default.Password);
            const QueryType queryType = QueryType.ArtistID;
            var result = SynoHelper.Search<SynoItem>(syno, artistId, queryType);
            Assert.IsTrue(result.Count() > 0);
            Assert.IsFalse(syno._queryLock);
        }

    }


    internal class SynoHelper
    {
        // This only works for UnitTesting ( Go figure why ? - Threads behave differently here !)
        internal static IEnumerable<T> Search<T>(AudioStationSession_Accessor session, string query, QueryType searchType)
        {
            IAsyncResult result = session.SearchAsync(query, searchType);
            result.AsyncWaitHandle.WaitOne();
            return (IEnumerable<T>)result.AsyncState;
        }
    }
}
